﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class DriverMissionStatusRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<DriverMissionStatus> GetAllData()
        {
            return from DriverMissionStatus in db.DriverMissionStatuses
                   orderby DriverMissionStatus.DriverMissionStatusCode descending
                   select DriverMissionStatus;
        }


        public IQueryable<DriverMissionStatus> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.DriverMissionStatuses.Where(p => p.DriverMissionStatusCode.Contains(searchString));
            if (searchField == "DriverMissionStatusDescription")
                ps = db.DriverMissionStatuses.Where(p => p.DriverMissionStatusDescription.Contains(searchString));
            if (searchField == "DriverMissionStatusCode")
                ps = db.DriverMissionStatuses.Where(p => p.DriverMissionStatusCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<DriverMissionStatus> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public DriverMissionStatus GetByID(string id)
        {
            return db.DriverMissionStatuses.SingleOrDefault(DriverMissionStatus => DriverMissionStatus.DriverMissionStatusCode == id);
        }

        //Inset
        public void Add(DriverMissionStatus DriverMissionStatus)
        {
            //if (DriverMissionStatus.StatusID == 0 )
            //    DriverMissionStatus.StatusID = 1;
            DriverMissionStatus.InsertedUser = HttpContext.Current.User.Identity.Name;
            DriverMissionStatus.InsertedDate = DateTime.Now;
            DriverMissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            DriverMissionStatus.UpdatedDate = DateTime.Now;
            DriverMissionStatus.DataVersion = 1;

            db.DriverMissionStatuses.AddObject(DriverMissionStatus);
        }

        //Update
        public void Update(DriverMissionStatus DriverMissionStatus)
        {

            DriverMissionStatus originalDriverMissionStatus = GetByID(DriverMissionStatus.DriverMissionStatusCode);

            DriverMissionStatus.InsertedUser = originalDriverMissionStatus.InsertedUser;
            DriverMissionStatus.InsertedDate = originalDriverMissionStatus.InsertedDate;
            DriverMissionStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            DriverMissionStatus.UpdatedDate = DateTime.Now;
            DriverMissionStatus.DataVersion = ++originalDriverMissionStatus.DataVersion;
  
            db.DriverMissionStatuses.ApplyCurrentValues(DriverMissionStatus);
        }

        //Delete
        public void Delete(DriverMissionStatus DriverMissionStatus)
        {
            db.DriverMissionStatuses.DeleteObject(DriverMissionStatus);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}